#include<iostream>
#include<pthread.h>
#include<unistd.h>
#include<vector>
using namespace std;
#define NUM 3
struct ThreadData{
//   string tid;
  string tname;

};
int *p = nullptr;
char* toHex(pthread_t tid)
{
    char buff[128];
     snprintf(buff,sizeof(buff),"0x%x",tid);

     return buff;
}
__thread int g_val = 100;
void* t(void* args)
{
    ThreadData* td = static_cast<ThreadData*>(args);
    int i = 0;
    if(td->tname == "thread-2")
    {
        p = &i;
    }
    while(i < 10)
    {
        printf("pid: %d,name: %s,threadname: %s\n",getpid(),td->tname.c_str(),toHex(pthread_self()));
        g_val ++;
        cout << g_val << " &g_val" << &g_val << endl;
        cout << &i << endl;
        ++i;
        sleep(1);
    }
    delete td;
    return nullptr;
}
void InitThreadData(ThreadData* td,int i)
{
     td->tname = "thread-" + to_string(i);
}
int main()
{
    vector<pthread_t> v;
  for(int i = 0;i < NUM;++i)
  {
    pthread_t tid;
    ThreadData *td = new ThreadData;
    InitThreadData(td,i);
    pthread_create(&tid,nullptr,t,(void*)td);
    v.push_back(tid);
    sleep(1);
  }
 // cout << p << endl;
  for(int i = 0;i < NUM;++i)
  {
    pthread_join(v.at(i),nullptr);
  }
    return 0;
}